from django.template import Library
from projects.models import Objective

from projects import utils
from projects.models import Deliverable, Objective, Task

register = Library()

@register.inclusion_tag("objective_detail.html")
def show_objective_detail(objective):
  return {'objective_list': [objective]}
  
@register.inclusion_tag("objective_table.html")
def list_objectives(objectives):
  return {'objective_list': objectives.order_by('target_date', 'start_date')}

@register.inclusion_tag("deliverable_table.html")
def list_objective_deliverables(objective):
  set = objective.deliverable_set.order_by('target_date', 'start_date')
  return {'deliverable_list': set}

@register.inclusion_tag("deliverable_table.html")
def list_complete_deliverables(objective):
  set = objective.deliverable_set.exclude(completed_date = None).order_by('target_date', 'start_date')
  return {'deliverable_list': set}

@register.inclusion_tag("deliverable_table.html")
def list_incomplete_deliverables(objective):
  set = objective.deliverable_set.filter(completed_date = None).order_by('target_date', 'start_date')
  return {'deliverable_list': set}

@register.inclusion_tag("rating.html")
def objective_status(objective):
  assert isinstance(objective, Objective)
  incomplete_tasks = Task.objects.filter(deliverable__objective__id = objective.id, completed_date = None)
  incomplete_deliverables = Deliverable.objects.filter(objective__id = objective.id, completed_date = None)
  objectives = Objective.objects.filter(id = objective.id)
  return utils.get_ratings("objective", incomplete_tasks, incomplete_deliverables, objectives)

